<template>
  <div>
    <el-form
      label-width="auto"
      label-position="right"
      :model="user"
      :rules="rules"
      ref="resetForm"
      class="reset-form"
    >
      <el-form-item label="username:" class="form-login-item" prop="username">
        <el-input v-model="user.username" placeholder="请输入用户名"></el-input
        ><br />
      </el-form-item>
      <el-form-item label="email:" class="form-login-item" prop="email">
        <el-input placeholder="请输入邮箱" v-model="user.email"></el-input
        ><br />
      </el-form-item>
      <div class="login-button">
        <el-button type="primary" @click="reset('resetForm')">Reset</el-button>
        <el-button type="primary" @click="toLogin">toLogin</el-button>
      </div>
    </el-form>
  </div>
</template>
<script>
import { resetCommon } from "@/service.js";
export default {
  name: "Reset",
  data() {
    return {
      user: {
        username: "",
        email: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 5,
            max: 10,
            message: "长度在 5 到 10 个字符",
            trigger: "blur",
          },
        ],
        email: [
          {
            type: "email",
            required: true,
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"],
          },
        ],
      },
    };
  },
  methods: {
    toLogin() {
      this.$emit("toLogin")
    },
    async reset(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const res = await resetCommon(this.user);
          if (res.data.code == 0) {
            this.$message.success(res.data.message + "请重新登录");
            // this.$router.push({ path: "/login" });
            this.toLogin();
          }
        } else {
          this.$message.warning("请输入正确的用户名或密码");
          return false;
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
@keyframes firstDisplay {
  0% {
    opacity: 0;
  }

  100% {
    opacity: 1;
  }
}
.reset-form {
  animation: firstDisplay 1.5s ease-in-out 1;
  .login-button {
    text-align: right;
  }
}
</style>
